import { defineStore } from 'pinia'
import db from '../utils/localstorage'
import { service } from '../request/index.js'
import { log } from '../utils/log.js'

export const useAccountStore = defineStore('account', {
  state: () => ({
    user: null
  }),
  actions: {
    updateUser(user) {
      this.user = user
      db.save('USER', user)
    },
    async getUser() {
      try {
        const { data } = await service.get('/accounts')
        if (data.ok) {
          this.updateUser(data.result)
          return this.user
        }
      } catch (error) {
        log(error)
      }
    }
  },
})
